<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <!-- <el-form-item label="裁判员管理的项目名称" prop="refereesXmName">
        <el-input
          v-model="queryParams.refereesXmName"
          placeholder="请输入裁判员管理的项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="裁判员名称" prop="refereesName">
        <el-input
          v-model="queryParams.refereesName"
          placeholder="请输入裁判员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sportferees:ferees:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sportferees:ferees:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sportferees:ferees:remove']"
        >删除
        </el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['sportDeclareInformation:declareInformation:import']"
        >导入裁判员
        </el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sportferees:ferees:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="fereesList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="裁判员id" align="center" prop="refereesUid" /> -->
      <!-- <el-table-column label="裁判员管理的项目id" align="center" prop="refereesXmUid" /> -->
      <!-- <el-table-column label="裁判员管理的项目名称" align="center" prop="refereesXmName" /> -->
      <el-table-column label="裁判员名称" align="center" prop="refereesName" />
      <el-table-column
        label="裁判员等级"
        align="center"
        prop="refereesLevel"
        sortable
      >
        <template slot-scope="scope">
          <span v-if="scope.row.refereesLevel == 0">
            <el-button type="primary" round>国家一级</el-button>
          </span>
          <span v-if="scope.row.refereesLevel == 1">
            <el-button type="primary" round>国家二级</el-button>
          </span>
          <span v-if="scope.row.refereesLevel == 2">
            <el-button type="primary" round>国家三级</el-button>
          </span>
          <span v-if="scope.row.refereesLevel == 3">
            <el-button type="primary" round>国家特级</el-button>
          </span>
          <span v-if="scope.row.refereesLevel == 4">
            <el-button type="primary" round>其他</el-button>
          </span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="裁判员头像" align="center" prop="sortList">
        <img
            :src="sortList"
            style="width: 120px; height: 120px"
            class="img-circle img-lg"
          />
              </el-table-column> -->
      <!-- <div v-for="(item, index) in sortList" :key="index">
          <img
            :src="item"
            :key="index"
            style="width: 120px; height: 120px"
            class="img-circle img-lg"
          />
        </div> -->
      <el-table-column label="裁判员头像" align="center">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.refereesphoto"
            :preview-src-list="[scope.row.refereesphoto]"
            style="width: 120px; height: 120px"
            class="img-circle img-lg"
          >
          </el-image>
        </template>
      </el-table-column>

      <el-table-column
        label="裁判员状态"
        align="center"
        prop="refereesStatus"
        sortable
      >
        <template slot-scope="scope">
          <span v-if="scope.row.refereesStatus == 0">
            <el-button type="primary" :loading="true" round>正常</el-button>
          </span>
          <span v-if="scope.row.refereesStatus == 1">
            <el-button type="danger" round>停职</el-button>
          </span>
        </template>
      </el-table-column>

      <el-table-column label="裁判员类型" align="center" prop="refereesXmType">
      </el-table-column>

      <el-table-column label="联系电话" align="center" prop="refereesiphone" />

      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sportferees:ferees:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sportferees:ferees:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改裁判员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="560px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="99px">
        <!-- <el-form-item label="裁判员管理的项目id" prop="refereesXmUid">
          <el-input v-model="form.refereesXmUid" placeholder="请输入裁判员管理的项目id" />
        </el-form-item> -->
        <!-- <el-form-item label="裁判员管理的项目名称" prop="refereesXmName">
          <el-input v-model="form.refereesXmName" placeholder="请输入裁判员管理的项目名称" />
        </el-form-item> -->
        <el-form-item label="裁判员名称" prop="refereesName">
          <el-input
            v-model="form.refereesName"
            placeholder="请输入裁判员名称"
          />
        </el-form-item>

        <el-form-item label="裁判员等级" prop="refereesLevel">
          <el-select
            v-model="form.refereesLevel"
            placeholder="请输入裁判员等级"
          >
            <el-option label="国家一级" value="0" />
            <el-option label="国家二级" value="1" />
            <el-option label="国家三级" value="2" />
            <el-option label="国家特级" value="3" />
            <el-option label="其他" value="4" />
          </el-select>
        </el-form-item>

        <!-- 裁判员头像上传 -->
        <el-form-item label="上传头像" prop="refereesphoto">
          <el-upload
            ref="uploadds"
            class="upload-demo"
            action="#"
            :before-upload="beforeUpload"
            :http-request="uploadDemo"
            v-model="form.refereesphoto"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传图片文件,且不超过5M
            </div>
          </el-upload>
        </el-form-item>

        <el-form-item label="裁判员状态" prop="refereesStatus" sortable>
          <el-select
            v-model="form.refereesStatus"
            placeholder="请输入裁判员状态"
          >
            <el-option label="正常" value="0" />
            <el-option label="停职" value="1" />
          </el-select>
        </el-form-item>

        <el-form-item label="裁判员类型" prop="refereesXmType">
          <el-input
            v-model="form.refereesXmType"
            placeholder="请输入裁判员类型"
          />
        </el-form-item>

        <el-form-item label="联系电话" prop="refereesiphone">
          <el-input v-model="form.refereesiphone" placeholder="请输入备注" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 裁判员导入 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="500px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <!-- <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link> -->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style scoped lang="scss">
</style>

<script>
import { getToken } from "@/utils/auth";
import {
  listFerees,
  getFerees,
  delFerees,
  addFerees,
  updateFerees,
  Fereesavatar,
} from "@/api/sportferees/ferees";

export default {
  name: "Ferees",
  data() {
    return {
      fileList: [],
      url: "",
      sortList: [], //图片数组
      //refereesphoto: "", //裁判员头像
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 裁判员表格数据
      fereesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        refereesXmName: null,
        refereesName: null,
        refereesXmType: null,
        refereesStatus: null,
        refereesphoto: null, //裁判员头像
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      // 裁判员导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的学生成绩数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/sportferees/ferees/importData",
      },
    };
  },

  created() {
    this.getList();
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "裁判员信息导入";
      this.upload.open = true;

    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    /** 查询裁判员头像上传 */
    //上传预处理
    beforeUpload(file) {
      // console.log(file)
      //判断上传大小不大于5M
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isLt5M) {
        // this.$message.error("上传图片大小不能超过5MB!");
        this.$modal.msgError("上传图片大小不能超过5MB!");
        this.cancel();
        return false;
      }
      let fileName = file.name;
      let uid = file.uid;
      let pos = fileName.lastIndexOf(".");
      let lastName = fileName.substring(pos, fileName.length);
      if (
        lastName.toLowerCase() !== ".png" &&
        lastName.toLowerCase() !== ".jpg"
      ) {
        this.$modal.msgError("上传图片文件后缀必须为 .png .jpg");
        this.cancel();
        // this.msgError("文件必须为 .png .jpg");
        return false;
      }
    },
    uploadDemo(data) {
      let formData = new FormData();
      formData.append("refereesphoto", data.file);
      Fereesavatar(formData).then((response) => {
        console.log(response.refereesphoto)
        //this.form.refereesphoto=process.env.VUE_APP_BASE_API+this.form.refereesphoto;
        this.form.refereesphoto = process.env.VUE_APP_BASE_API + response.refereesphoto;
      });
    },

    /** 查询裁判员列表 */
    getList() {
      this.loading = true;
      listFerees(this.queryParams).then((response) => {
        // for (let i = 0; i < response.rows.length; i++) {
        //   // this.fileList.push()
        //   this.sortList = response.rows[i].refereesphoto;
        // }
        // console.log(this.fileList);
        this.fereesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        refereesUid: null,
        refereesXmUid: null,
        refereesXmName: null,
        refereesphoto: null, //裁判员头像
        refereesName: null,
        refereesLevel: null,
        refereesXmType: null,
        refereesStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.refereesUid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$nextTick(() => {
        //解决回调延迟到下次DOM更新循环之后执行。在修改数据之后立即使用它，然后等待DOM更新。
        this.$refs["uploadds"].clearFiles(); //清除新增头像的缓存
      });
      this.open = true;
      this.title = "添加裁判员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row.refereesUid)  //获取当前裁判员id
      this.reset();
      this.$nextTick(() => {
        this.$refs["uploadds"].clearFiles(); //清除修改头像的缓存
      });
      const refereesUid = row.refereesUid || this.ids;
      getFerees(refereesUid).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改裁判员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.refereesUid != null) {
            updateFerees(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFerees(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const refereesUids = row.refereesUid || this.ids;
      this.$modal
        .confirm('是否确认删除裁判员编号为"' + refereesUids + '"的数据项？')
        .then(function () {
          return delFerees(refereesUids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sportferees/ferees/export",
        {
          ...this.queryParams,
        },
        `ferees_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>


